
; 是否允许安装在根目录下
AllowRootDirInstall false

InstallDir "$PROGRAMFILES64\cnao\webrtcdemo"
InstallDirRegKey HKLM "${PRODUCT_UNINST_KEY}" "UninstallWebrtcDemo"

; HM NIS Edit Wizard helper defines
!include "MUI2.nsh"

; MUI Settings
!define MUI_ABORTWARNING

; 欢迎页面
!insertmacro MUI_PAGE_WELCOME

; 初始化开始菜单
!define PRODUCT_STARTMENU_REGVAL "NSIS:StartMenuDir"
!define MUI_STARTMENUPAGE_NODISABLE
Var StartMenuFolder
!insertmacro MUI_PAGE_STARTMENU Application $StartMenuFolder

ShowInstDetails show
ShowUnInstDetails show
SpaceTexts show

; HM NIS Edit Wizard helper defines custom install default dir
!macro preInit
    SetRegView 64
    WriteRegExpandStr HKLM "${INSTALL_REGISTRY_KEY}" InstallLocation "$PROGRAMFILES64\cnao\webrtcdemo"
    WriteRegExpandStr HKCU "${INSTALL_REGISTRY_KEY}" InstallLocation "$PROGRAMFILES64\cnao\webrtcdemo"
    SetRegView 32
    WriteRegExpandStr HKLM "${INSTALL_REGISTRY_KEY}" InstallLocation "$PROGRAMFILES\cnao\webrtcdemo"
    WriteRegExpandStr HKCU "${INSTALL_REGISTRY_KEY}" InstallLocation "$PROGRAMFILES\cnao\webrtcdemo"
!macroend

!macro customInstall

    System::Call 'Kernel32::SetEnvironmentVariable(t "__COMPAT_LAYER", t "WINXPSP3")' 
    System::Call 'Kernel32::SetEnvironmentVariable(t "__COMPAT_LAYER", i 0)' 

    ; 写入开始菜单
    CreateDirectory "$SMPROGRAMS\$StartMenuFolder"
    !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
    CreateShortCut "$SMPROGRAMS\$StartMenuFolder\${PRODUCT_NAME}.lnk" "$INSTDIR\${PRODUCT_NAME}.exe"
    CreateShortCut "$SMPROGRAMS\$StartMenuFolder\卸载 ${PRODUCT_NAME}.lnk" "$INSTDIR\Uninstall ${PRODUCT_NAME}.exe"
    !insertmacro MUI_STARTMENU_WRITE_END

    SetRegView 64
    SetOutPath $INSTDIR
    WriteRegStr HKCR "cnaoprotocol" "" ""
    WriteRegStr HKCR "cnaoprotocol" "URL Protocol" "$INSTDIR\${PRODUCT_NAME}.exe"
    WriteRegStr HKCR "cnaoprotocol\DefaultIcon" "" "$INSTDIR\${PRODUCT_NAME}.exe,1"
    WriteRegStr HKCR "cnaoprotocol\shell\open\command" "" '"$INSTDIR\${PRODUCT_NAME}.exe" "%1"'
    nsExec::Exec "cmd /c CheckNetIsolation LoopbackExempt -a -n=Microsoft.MicrosoftEdge_8wekyb3d8bbwe"

    SetRegView 32
    SetOutPath $INSTDIR
    WriteRegStr HKCR "cnaoprotocol" "" ""
    WriteRegStr HKCR "cnaoprotocol" "URL Protocol" "$INSTDIR\${PRODUCT_NAME}.exe"
    WriteRegStr HKCR "cnaoprotocol\DefaultIcon" "" "$INSTDIR\${PRODUCT_NAME}.exe,1"
    WriteRegStr HKCR "cnaoprotocol\shell\open\command" "" '"$INSTDIR\${PRODUCT_NAME}.exe" "%1"'
    nsExec::Exec "cmd /c CheckNetIsolation LoopbackExempt -a -n=Microsoft.MicrosoftEdge_8wekyb3d8bbwe"
!macroend

!macro customUnInstall
    SetShellVarContext current
    MessageBox MB_YESNO "删除用户数据和配置？" \
        /SD IDNO IDNO Skipped IDYES Accepted

        Accepted:
            RMDir /r "$APPDATA\${APP_FILENAME}"
            !ifdef APP_PRODUCT_FILENAME
            RMDir /r "$APPDATA\${APP_PRODUCT_FILENAME}"
            !endif
            # electron use package.json name for cache,indexdb etc.
            !ifdef APP_PACKAGE_NAME
            RMDir /r "$APPDATA\${APP_PACKAGE_NAME}"
            !endif
            !ifdef PRODUCT_NAME
            RMDir /r "$APPDATA\${PRODUCT_NAME}"
            !endif
            Goto done
        Skipped:
            Goto done
        done:
    !insertmacro MUI_STARTMENU_GETFOLDER Application $StartMenuFolder
    Delete "$SMPROGRAMS\$StartMenuFolder\${PRODUCT_NAME}.lnk"
    Delete "$SMPROGRAMS\$StartMenuFolder\卸载 ${PRODUCT_NAME}.lnk"
    RMDir "$SMPROGRAMS\$StartMenuFolder"
    RMDir "$INSTDIR"
	DeleteRegValue HKCU "SOFTWARE\Microsoft\Windows\CurrentVersion\Run" "com.easemob.webrtcdemo"
	DeleteRegKey HKCR "cnaoprotocol"
    
!macroend